# -*- coding: utf-8 -*-

from qgis.core import (
    QgsGeometry,
    QgsWkbTypes,
    Qgis,
    QgsPointXY
)

from qgis.gui import (
    QgsRubberBand,
    QgsMapToolEmitPoint,
    QgsMapMouseEvent
)

from PyQt5.QtCore import Qt,pyqtSignal
from PyQt5.QtGui import QColor

class SelectFeatureTool(QgsMapToolEmitPoint):
    # 绘制信号
    # str 绘制行为
    # Qgis.GeometryType 绘制类型
    # QgsGeometry 绘制图形容器
    drawGeometry = pyqtSignal(str,Qgis.GeometryType,QgsGeometry)

    def __init__(self,action, canvas):
        super().__init__(canvas)
        self.__action = action
        self.__canvas = canvas
        self.__crs = canvas.mapSettings().destinationCrs()
        
        self.__m_rabberband = QgsRubberBand(canvas,QgsWkbTypes.PolygonGeometry)
        self.__m_rabberband.setFillColor(QColor(0,0,0,0))  # 白色填充，透明度为64（可调整）
        self.__m_rabberband.setStrokeColor(Qt.gray)
        self.__m_rabberband.setWidth(2)

        self.__m_isselecting = False

        self.__m_start_point = None
        self.__m_end_point = None
        
    def canvasPressEvent(self, e):
        if self.__m_isselecting == False:
            self.__m_isselecting = True
            self.__canvas.scene().addItem(self.__m_rabberband)

            # 拾取开始勾绘点
            self.__m_start_point = e.mapPoint()
            # 拾取结束勾绘点
            self.__m_end_point = e.mapPoint()

            # 将点集转换为图形
            geometry = QgsGeometry.fromPolygonXY([
                    [
                        self.__m_start_point,
                        self.__m_start_point
                    ]
                ])
            
            # 更新图形渲染
            self.__m_rabberband.setToGeometry(geometry,self.__crs)
            
    def canvasMoveEvent(self, e):
        if self.__m_isselecting == True:
            # 拾取结束勾绘点
            self.__m_end_point = e.mapPoint()

            # 将点集转换为图形
            geometry = QgsGeometry.fromPolygonXY([
                    [
                        QgsPointXY(self.__m_start_point.x(),self.__m_start_point.y()),
                        QgsPointXY(self.__m_end_point.x(),self.__m_start_point.y()),
                        QgsPointXY(self.__m_end_point.x(),self.__m_end_point.y()),
                        QgsPointXY(self.__m_start_point.x(),self.__m_end_point.y()),
                        QgsPointXY(self.__m_start_point.x(),self.__m_start_point.y())
                    ]
                ]) 
            
            # 更新图形渲染
            self.__m_rabberband.setToGeometry(geometry,self.__crs)

    def canvasReleaseEvent(self, e):
        if self.__m_isselecting == True:
            self.__m_isselecting = False
            
            # 将点集转换为图形
            geometry = QgsGeometry.fromPolygonXY([
                    [
                        QgsPointXY(self.__m_start_point.x(),self.__m_start_point.y()),
                        QgsPointXY(self.__m_end_point.x(),self.__m_start_point.y()),
                        QgsPointXY(self.__m_end_point.x(),self.__m_end_point.y()),
                        QgsPointXY(self.__m_start_point.x(),self.__m_end_point.y()),
                        QgsPointXY(self.__m_start_point.x(),self.__m_start_point.y())
                    ]
                ]) 
            
            # 将数据发送到信号中
            self.drawGeometry.emit(self.__action,Qgis.GeometryType.Polygon,geometry)

            # 从画布中移除图形
            self.__canvas.scene().removeItem(self.__m_rabberband)

    def deactivate(self):
        #self.rubber_band.reset(QgsWkbTypes.PolygonGeometry)
        super().deactivate()